package com.example.dream.learnandroid.geoquiz;

import android.app.Activity;
import android.content.Intent;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dream.learnandroid.R;

public class QuizActivity extends AppCompatActivity {

    private static final String TAG = "QuizActivity";
    private static final String KEY_INDEX = "index";
    private static final String KEY_IS_CHEATER = "is_cheater";
    private static final int    REQUEST_CODE_CHEAT = 0;

    private Button mBtnTrue;
    private Button mBtnFalse;
    private Button mBtnPrev;
    private Button mBtnNext;
    private Button mBtnCheat;
    private ImageButton mImagebtnPrev;
    private ImageButton mImagebtnNext;
    private TextView mTextViewQuestion;

    private Question[] mQuestionBank = new Question[] {
            new Question(R.string.question_australia,true),
            new Question(R.string.question_Ocean,true),
            new Question(R.string.question_mideast, false),
            new Question(R.string.question_africa, false),
            new Question(R.string.question_asia, true)
    };

    private boolean[] mIsCheater = new boolean[mQuestionBank.length];
    private int mCurrentIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate(Bundle) called");
        setContentView(R.layout.activity_quiz);

        if(savedInstanceState != null) {
            mCurrentIndex = savedInstanceState.getInt(KEY_INDEX);
            mIsCheater = savedInstanceState.getBooleanArray(KEY_IS_CHEATER);
        }
        mTextViewQuestion = findViewById(R.id.question_text_view);
        mTextViewQuestion.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showNextQuestion();
            }
        });
        this.updateQuestion();

        mBtnTrue = findViewById(R.id.btn_true);
        mBtnTrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAnswer(true);
            }
        });

        mBtnFalse = findViewById(R.id.btn_false);
        mBtnFalse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkAnswer(false);
            }
        });

        mBtnPrev = findViewById(R.id.btn_prev);
        mBtnPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "mBtnPrev onClick: ");
                showPrevQusetion();
            }
        });
        mBtnNext = findViewById(R.id.btn_next);
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "mBtnNext onClick: ");
                showNextQuestion();
            }
        });

        mBtnCheat = findViewById(R.id.btn_cheat);
        mBtnCheat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
                //通过指定Content 与 Class 对象，然后调用intent 的构造方法创建Intent 创建的是显式intent
                Intent intent = CheatActivity.newIntent(QuizActivity.this, answerIsTrue);
                //从子Activity 获取返回信息
                startActivityForResult(intent, REQUEST_CODE_CHEAT);
            }
        });

        mImagebtnPrev = findViewById(R.id.imgBtn_prev);
        mImagebtnPrev.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPrevQusetion();
            }
        });

        mImagebtnNext = findViewById(R.id.imgBtn_next);
        mImagebtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showNextQuestion();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG, "onActivityResult: ...... resultCode" + resultCode);

        if(resultCode != Activity.RESULT_OK){
            return;
        }

        if(requestCode == REQUEST_CODE_CHEAT){
            if(data == null){
                return;
            }
            mIsCheater[mCurrentIndex] = CheatActivity.wasAnswerShown(data);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.d(TAG, "onStart() called");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.d(TAG, "onResume() called");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause() called");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d(TAG, "onSaveInstanceState  called");
        outState.putInt(KEY_INDEX, mCurrentIndex);
        outState.putBooleanArray(KEY_IS_CHEATER, mIsCheater);
    }

    @Override
    protected void onStop() {
        super.onStop();
        Log.d(TAG, "onStop() called");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d(TAG, "onDestroy() called");
    }

    private void showPrevQusetion(){
        mCurrentIndex = (mCurrentIndex -1 + mQuestionBank.length) % mQuestionBank.length;
        updateQuestion();
    }

    private void showNextQuestion(){
        mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
        updateQuestion();
    }
    private void updateQuestion(){
        int question = mQuestionBank[mCurrentIndex].getTextResId();
        mTextViewQuestion.setText(question);
    }

    private void checkAnswer(boolean userPressedTrue){
        boolean answerIsTrue = mQuestionBank[mCurrentIndex].isAnswerTrue();
        int messageResId;

        if(mIsCheater[mCurrentIndex]){
            messageResId = R.string.judgment_toast;
        }else{
            messageResId = userPressedTrue == answerIsTrue ? R.string.correct_toast : R.string.incorrec_toast;
        }
        Toast.makeText(QuizActivity.this, messageResId, Toast.LENGTH_SHORT).show();

    }
}

// 在桌面启动器点击LearnAndroid 应用时，操作系统并没有启动应用，而是启动应用的一个Activity
// 它启动了应用的launcher Activity。
// launcher Activity 声明在intent-filter 元素节点下
